const express = require("express")
const db = require("./db/index1")
const app = express()
const port = 3000

//配置body-parser
// 只要加上这个配置，则在req请求对象上会多出来一个属性：body
// 因此可以直接通过req.body来获取表单post请求体数据
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//配置跨域以及请求头
app.use((req, res, next) => {
    //设置请求头
    res.set({
        'Access-Control-Allow-Credentials': true,
        'Access-Control-Max-Age': 1728000,
        'Access-Control-Allow-Origin': req.headers.origin || '*',
        'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type',
        'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS',
        'Content-Type': 'application/json; charset=utf-8'
    })
    req.method === 'OPTIONS' ? res.status(204).end() : next()
})


app.get('/', (req, res) => {
    let sql = `select * from user`
    db(sql, (err, result) => {
        if (err) throw err;
        res.send({
            data: {
                code: 200,
                msg: result
            }
        })
    })
})


// 查询接口
app.get('/search', (req, res) => {
    let data = req.query
    let sql = ''
    if (Object.keys(data).length) {
        sql = `select * from user where name= "${data.name}"`
    } else {
        sql = 'select * from user'
    }
    db(sql, (err, result) => {
        if (err) throw err;
        res.send({
            data: {
                code: 200,
                msg: result
            }
        })
    })
})

// 登录接口
app.get('/login', (req, res) => {
    let data = req.query
    console.log(data);
    let sql = `select * from user where name= "${data.name}" and password = "${data.password}" `;
    db(sql, (err, result) => {
        if (err) throw err;
        if (Object.keys(result).length) {
            res.send({
                data: {
                    code: 200,
                    msg: `登陆成功`,
                    token: '123'
                }
            })
        } else {
            let sql = `select * from user where name= "${data.name}"`
            db(sql, (err, result) => {
                if (err) throw err;
                if (Object.keys(result).length) {
                    res.send({
                        data: {
                            code: 404,
                            msg: '用户名或密码错误'
                        }
                    })
                } else {
                    res.send({
                        data: {
                            coe: 401,
                            msg: '此用户不存在'
                        }
                    })
                }
            })
        }
    })
})


// 注册接口
app.all('/sign', (req, res) => {
    let data = ''
    if (Object.keys(req.query).length) {
        data = req.query
    } else {
        data = req.body
    }
    console.log(data);
    let sql = `select * from user where name= "${data.name}"`;
    db(sql, (err, result) => {
        if (err) throw err;
        if (Object.keys(result).length) {
            res.send({
                code: 404,
                msg: '此用户已存在'
            })
        } else {
            let sql = `INSERT INTO user (name, password) VALUES ("${data.name}","${data.password}")`
            db(sql, (err, result) => {
                if (err) throw err
                res.send({
                    code: 200,
                    msg: '注册成功'
                })
            })
        }
    })
})

app.listen(port, () => {
    console.log(`http://127.0.0.1:${port}`)
})